package com.bdqn.repository;

import com.bdqn.pojo.User;
import com.bdqn.vo.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Component
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    public List<User> findByUsrName(String usrName);

    public Long countByUsrName(String usrName);

    public List<User> findByUsrNameLike(String usrName);

    /*@Query("select u from User u where u.usrRoleId=?1")
    public List<User> findByRoleId(Long roleId);*/
/*@Query(value = "select * from sys_user where usr_role_id=?1",nativeQuery = true)
    public List<User> findByRoleId(Long roleId);*/
    @Query("select u from User u where u.role.roleId=:roleId")
    public List<User> findByRoleId(@Param("roleId") Long roleId);

    @Transactional(timeout = 10)
    @Modifying
    @Query("update User u set u.usrName=?1 where u.usrId=?2")
    public int modifyNameById(String usrName, Long usrId);

    //此方法对应User实体类中的命名查询
//如果希望根据接口方法名来实现数据库操作，则命名为：findUserByUsrName
    public List<User> findUsersByName(String usrName);

    @Query("select u from User u where u.role.roleId=?1")
    Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);

    @Query("select u.usrId as usrId, u.usrName as usrName,u.usrPassword as usrPassword,u.role.roleId as usrRoleId,u.usrFlag as usrFlag,r.roleName as roleName from User u,Role r where u.role.roleId=r.roleId and u.usrId=?1")
    public UserInfo getUserInfo(Long usrId);


}
